package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    //添加购物车
    @Override
    public void addCart(ShoppingCartDTO dto) {

        //创建shopping_cart对象
        ShoppingCart shoppingCart = new ShoppingCart();
        // 拷贝属性值
        BeanUtils.copyProperties(dto, shoppingCart);
        //1.判断商品是否存在购物车
        //只查看当前用户自己的购物车
        shoppingCart.setUserId(BaseContext.getCurrentId());
       ShoppingCart cart= shoppingCartMapper.selectBy(shoppingCart);
       if(cart==null){//代表购物车没有该商品数据
           //2.补充却失的属性值
           //判断是新增套餐还是餐品
           if(dto.getDishId() != null){//代表新增的是菜品
               //根据菜品id查询菜品表
               log.info("新增的数据是菜品");
               Dish dish = dishMapper.getById(dto.getDishId());
                   shoppingCart.setName(dish.getName());
                   shoppingCart.setAmount(dish.getPrice());
                   shoppingCart.setImage(dish.getImage());

               log.info("数据检查{}",shoppingCart);

           }else{//新增的是套餐
               //根据套餐id查套餐表
               SetmealVO setmeal = setmealMapper.getSetmealById(dto.getSetmealId());
               shoppingCart.setName(setmeal.getName());
               shoppingCart.setAmount(setmeal.getPrice());
               shoppingCart.setImage(setmeal.getImage());


           }
           shoppingCart.setNumber(1);//数量：
           shoppingCart.setCreateTime(LocalDateTime.now());
           //3.将商品数据存shopping_cart表中
           shoppingCartMapper.insert(shoppingCart);
       }else{//代表购物车有该商品
           //4.将原来的购物车商品数量+1，调用mapper更新方法
           log.info("购物车已有，仅添加数量");
           cart.setNumber(cart.getNumber()+1);
           shoppingCartMapper.update(cart);
        }
        //最终目的：将用户添加的商品添加到shopping_cart表中
    }

    @Override
    //查看购物车
    public List<ShoppingCart> listCart() {
        return shoppingCartMapper.list(BaseContext.getCurrentId());
    }
    //清空购物车
    @Override
    public void clean() {
        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }
    //删除购物车中的一个商品
    @Override

    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        ShoppingCart shoppingCarts=shoppingCartMapper.selectBy(shoppingCart);
        log.info("检查商品信息{}",shoppingCart);
        if(shoppingCarts.getNumber()>1){
            //删除购物车中的一个商品
            shoppingCartMapper.sub(shoppingCartDTO);
        }else{
            //将该商品移出购物车
            shoppingCartMapper.delete(shoppingCartDTO.getDishId());

        }

    }
}
